import { user } from '@/api'
export default {
    state: {
        userinfo: {
            cname: "",
            image: "",
            mobile: "",
            add: "",
            remark: ""
        },
        zhichan: {
            cjf: 0,
            ajf: 0,
            bjf: 0
        }
    },
    getters: {
        info(state) {
            return state.userinfo
        },
        zhichans(state) {
            return state.zhichan
        }
    },
    mutations: {
        setinfo(state, res) {
            state.userinfo = Object.assign(state.userinfo, res)
        },
        setzhichan(state, res) {
            state.zhichan = Object.assign(state.zhichan, res)
        }
    },
    actions: {
        addinfo(context, res) {
            return new Promise((resolve, rereject) => {
                user.info(res).then(data => {
                    if (data.code == '1000') {
                        context.commit('setinfo', data.result)
                        localStorage.setItem('cname', data.result.cname)
                        localStorage.setItem('mobile', data.result.mobile)
                    }
                    resolve(data)
                })
            })
        },
        addzhichan(context, res) {
            return new Promise((resolve, rereject) => {
                user.zhichan(res).then(data => {
                    if (data.code == 1000) {
                        context.commit('setzhichan', data.result)
                    }
                    resolve(data)
                })
            })
        },
        outlogins(context, res) {
            return new Promise((resolve, redirect) => {
                user.outlogin(res).then(data => {
                    if (data.code == 1000) {
                        window.localStorage.clear()
                        let userinfo = {
                            cname: "",
                            image: "",
                            mobile: "",
                            add: "",
                            remark: ""
                        };
                        context.commit('setinfo', userinfo)
                    }
                    resolve(data)
                })
            })
        },
        clearlocal() {

        }
    }
};